package com.boarsoft.message.activemq;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AmqDestination {
	private static final Logger log = LoggerFactory.getLogger(AmqDestination.class);

	public static final short TYPE_QUEUE = 0;
	public static final short TYPE_TOPIC = 1;

	/** */
	protected AmqProducer producer;
	/** */
	protected AmqConsumer consumer;
	/** */
	protected Destination destination;

	/** */
	protected String code;
	/** */
	protected short type = TYPE_QUEUE;

	public void close() {
		if (producer != null) {
			try {
				producer.close();
			} catch (JMSException e) {
				log.warn("Error on close AmqProducer {}", producer, e);
			}
		}
		if (consumer != null) {
			try {
				consumer.close();
			} catch (JMSException e) {
				log.warn("Error on close AmqConsumer {}", consumer, e);
			}
		}
	}

	public void init(Session session, String code) throws JMSException {
		this.code = code;
		switch (type) {
		case TYPE_QUEUE:
			destination = session.createQueue(code);
			break;
		case TYPE_TOPIC:
			destination = session.createTopic(code);
			break;
		}
		if (producer != null) {
			producer.init(session, destination);
		}
		if (consumer != null) {
			consumer.init(session, destination);
		}
	}

	public AmqProducer getProducer() {
		return producer;
	}

	public AmqConsumer getConsumer() {
		return consumer;
	}

	public short getType() {
		return type;
	}

	public void setType(short type) {
		this.type = type;
	}

	public String getCode() {
		return code;
	}

	public void setProducer(AmqProducer producer) {
		this.producer = producer;
	}

	public void setConsumer(AmqConsumer consumer) {
		this.consumer = consumer;
	}
}
